package com.mauth.web.config;


import com.mauth.common.encoder.AesPasswordEncoder;
import com.mauth.web.config.filter.JwtAuthorizationFilter;
import com.mauth.web.service.JwtService;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter implements AuthenticationEntryPoint {
    private static final String aesSecret = "fsafffffffffff";
    @Resource
    private JwtService jwtService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new AesPasswordEncoder(aesSecret);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtService));
        http.authorizeRequests()
                .antMatchers("/oauth2/*").permitAll()
                .antMatchers("/login/*").permitAll()
                .and().headers().frameOptions().disable()
                .and().csrf().disable()
                .cors().disable();
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
    }
}
